/*
 * File: INTERPOLATE_U16_U8.c
 *
 * Code generated for Simulink model 'SOCE'.
 *
 * Model version                  : 3.241
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Tue May 10 15:48:28 2022
 */

#include "rtwtypes.h"
#include "INTERPOLATE_U16_U8.h"

/* Lookup Interpolation INTERPOLATE_U16_U8 */
void INTERPOLATE_U16_U8(uint16_T *pY, uint16_T yL, uint16_T yR, uint8_T x,
                        uint8_T xL, uint8_T xR)
{
    uint32_T bigProd;
    uint16_T yDiff;
    uint8_T xNum;
    uint8_T xDen;
    *pY = yL;

    /* If x is not strictly between xR and xL
     * then an interpolation calculation is not necessary x == xL
     * or not valid.  The invalid situation is expected when the input
     * is beyond the left or right end of the table.  The design is
     * that yL holds the correct value for *pY
     * in invalid situations.
     */
    if ((xR > xL) && (x > xL) )
    {
        xDen = xR;
        xDen = (uint8_T)((uint32_T)xDen - (uint32_T)xL);
        xNum = x;
        xNum = (uint8_T)((uint32_T)xNum - (uint32_T)xL);
        if (yR >= yL )
        {
            yDiff = yR;
            yDiff = (uint16_T)((uint32_T)yDiff - (uint32_T)yL);
        }
        else
        {
            yDiff = yL;
            yDiff = (uint16_T)((uint32_T)yDiff - (uint32_T)yR);
        }

        bigProd = (uint32_T)yDiff * (uint32_T)xNum;

        {
            uint32_T rtb_u32_tmp;
            rtb_u32_tmp = xDen;
            yDiff = (uint16_T)(rtb_u32_tmp == 0U ? MAX_uint32_T : bigProd /
                               rtb_u32_tmp);
        }

        if (yR >= yL )
        {
            *pY = (uint16_T)((uint32_T)*pY + (uint32_T)yDiff);
        }
        else
        {
            *pY = (uint16_T)((uint32_T)*pY - (uint32_T)yDiff);
        }
    }
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
